package cn.domypay.mail.thread;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import cn.domypay.back.log.DoMyPayLogger;
import cn.domypay.mail.base.Pop3Biz;
import cn.domypay.mail.base.SwanetAuthenticate;
import cn.domypay.mail.bean.Mailbox;
import cn.domypay.mail.conf.DomypayMail;

public class Pop3Thread extends Thread{

	private static DoMyPayLogger logger = DoMyPayLogger.getLogger(Pop3Thread.class);
	
	private Mailbox mailbox;
	
	private Pop3Biz pop3Biz;
	
	private Session session;
	
	private Properties mailProps;
	
	public Pop3Thread(Mailbox mailbox)
	{
		this.mailbox = mailbox;
	}
	
	public void run()
	{
		try {
			check();
		} catch (MessagingException e) {
			// TODO �Զ���� catch ��
			logger.logInfo(e);
		}
	}
	public void check() throws MessagingException
	{
		pop3Biz = new Pop3Biz();
		
		mailProps = new Properties();
		
		if(DomypayMail.NEED_SSL.equals(mailbox.getPop3Ssl()))
		{
			mailProps = pop3Biz.getPop3_SslProp(mailbox);
			
		}else{
			
			mailProps = pop3Biz.getPop3Prop(mailbox);
		}
		SwanetAuthenticate pAuth = new SwanetAuthenticate(mailbox
				.getUsername(), mailbox.getPassword());
		session = Session.getInstance(mailProps,pAuth);
		
		pop3Biz.checkPop3(session, mailbox);
	}
	public void pop3Stop() throws NoSuchProviderException, MessagingException
	{
		
		session.getStore("pop3").close();
		this.interrupt();
		
	}
}
